
package org.jetnuke.dao.generic;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import net.jforum.JForumExecutionContext;
import net.jforum.dao.generic.AutoKeys;
import net.jforum.util.preferences.SystemGlobals;

import org.jetnuke.entities.FaqAnswer;

public class GenericFaqAnswerDAO extends AutoKeys implements
		org.jetnuke.dao.FaqAnswerDAO {

	/* (non-Javadoc)
	 * @see org.jetnuke.model.FaqAnswerModel#retrieveByPK(int)
	 */
	public FaqAnswer retrieveByPK(int id) throws Exception {
		PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("FaqAnswerModel.selectByID"));
		p.setInt(1, id);
		
		ResultSet rs = p.executeQuery();
		FaqAnswer s = new FaqAnswer();
		
		if (rs.next()) {
			fillFaqAnswerFromResultSet(s, rs);
		}
		
		rs.close();
		p.close();

		return s;
	}

	/**
	 * @param s
	 * @param rs
	 */
	private void fillFaqAnswerFromResultSet(FaqAnswer s, ResultSet rs)throws Exception {
		s.setANSWER(rs.getString("ANSWER"));
		s.setID(rs.getInt("ID"));
		s.setID_CAT(rs.getInt("ID_CAT"));
		s.setQUESTION(rs.getString("QUESTION"));
		
	}

	/* (non-Javadoc)
	 * @see org.jetnuke.model.FaqAnswerModel#deleteFaq(int)
	 */
	public void deleteFaq(int id) throws Exception{
		PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("FaqAnswerModel.deleteFaq"));
		p.setInt(1, id);
		p.executeUpdate();
		p.close();
	}

	/* (non-Javadoc)
	 * @see org.jetnuke.model.FaqAnswerModel#update(org.jetnuke.entities.FaqAnswer)
	 */
	public void update(FaqAnswer faq) throws Exception {
		PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("FaqAnswerModel.update"));
		
		p.setString(1, faq.getANSWER());
		p.setString(2,faq.getQUESTION());
		p.setInt(3,faq.getID_CAT());
		p.setInt(4,faq.getID());
		p.executeUpdate();
		p.close();

		
	}

	/* (non-Javadoc)
	 * @see org.jetnuke.model.FaqAnswerModel#addNew(org.jetnuke.entities.FaqAnswer)
	 */
	public int addNew(FaqAnswer fqa) throws Exception {
		PreparedStatement p = this.getStatementForAutoKeys("FaqAnswerModel.addNew");
		this.setAutoGeneratedKeysQuery(SystemGlobals.getSql("FaqAnswerModel.lastGeneratedFaqAnswerID"));
		initFaqAnswer(fqa, p);
		
		int id = this.executeAutoKeysQuery(p);
		p.close();		
		return id;
		
	}

	/**
	 * @param fqa
	 * @param p
	 */
	private void initFaqAnswer(FaqAnswer fqa, PreparedStatement p) throws Exception{
		p.setString(1,fqa.getANSWER());
		p.setInt(2,fqa.getID_CAT());
		p.setString(3,fqa.getQUESTION());
		
	}

	/* (non-Javadoc)
	 * @see org.jetnuke.model.FaqAnswerModel#selectByCat(int)
	 */
	public List selectByCat(int id) throws Exception {
		PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("FaqAnswerModel.selectByCat"));
		p.setInt(1,id);
		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();
		
		return list;
	}
	protected List processSelectAll(ResultSet rs)throws Exception {
		List list = new ArrayList();
		while (rs.next()) {
			FaqAnswer u = new FaqAnswer();
			fillFaqAnswerFromResultSet(u,rs);			
			list.add(u);
		}
		
		return list;
	}

	/* (non-Javadoc)
	 * @see org.jetnuke.model.FaqAnswerModel#deleteFaqsByCat(int)
	 */
	public void deleteFaqsByCat(int catid) throws Exception {
		PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("FaqAnswerModel.deleteFaqsByCat"));
		p.setInt(1, catid);
		p.executeUpdate();
		p.close();
		
	}

}
